<?php
/**
 * 房间管理
 * User: @671
 * Date: 2018/8/24 0024
 * Time: 16:34
 */
namespace app\publics\controller;

use think\facade\Request;
use think\Db;
use thinkcms\auth\library\Tree;
use thinkcms\auth\model\Menu;

use app\common\model\RoomModel;
use app\common\model\UserModel;

use app\common\utils\StatusUtils;


class Room extends Base
{
    protected $ur_model;

    public function __construct()
    {
        parent::__construct();
        $this->ur_model = new RoomModel();
        $this->u_model = new UserModel();
    }

    // 列表
    public function index()
    {
        $param = $this->ur_model->param_format(input('param.'));

        $map = [];

        if($param['status'] !=-1){
            $map['ur.status'] = $param['status'];
        }

        if(!empty($param['rid'])){
            $map['ur.rid'] = $param['rid'];
        }

        $count = (int)$this->ur_model
            ->alias('ur')
            ->Join('p_user u', 'ur.uid = u.uid')
            ->where($map)
            ->count();


        $list  = $this->ur_model
            ->alias('ur')
            ->Join('p_user u', 'ur.uid = u.uid')
            ->where($map)
            ->field('ur.*, u.uid,u.phone,u.username')
            ->order('ur.create_time asc')
            ->paginate($param['limit'], $count);

        $select_list = [];
        foreach (StatusUtils::$arry as $k => $v) {
            $select_list[$k] = explode('：', $v)[0];
        }

        $cur_page_sum =  $this->ur_model
            ->alias('ur')
            ->Join('p_user u', 'ur.uid = u.uid')
            ->where('ur.status',0)
            ->count();
        $all_page_sum = $this->ur_model
            ->alias('ur')
            ->Join('p_user u', 'ur.uid = u.uid')
            ->count();


        $this->assign([
            'param' => $param,
            'list'  => $list,
            'cur_page_sum' =>$cur_page_sum,
            'all_page_sum' =>$all_page_sum,
            'select_list' => $select_list,
            'limit' => select_limit($param['limit']),
            'count' => $count,
            'page'  => $list->render()
        ]);
        return $this->fetch();
    }

    //修改
    public function edit()
    {
        $post = input('param.');
        $status = $this->ur_model->where('id',$post['id'])->value('status');

        if($status == 0){
            $data = ['status'=>1];
        }else{
            $data = ['status'=>0];
        }
        $info = $this->ur_model->where('id',$post['id'])->update($data);

        if (!empty($info)) {
            $title = $status == 0 ? '正常' : '关闭';
            $msg = "管理员[ {$this->username} ] 更新房间状态 [ {$post['id']} ]".$title;
            $this->auth->createLog($msg, $title);
            $this->success('操作成功');
        } else {
            $this->error('操作失败，无任何数据变化');
        }
    }

    //批量审核
    public function status_update()
    {
        $post = input('param.');

        if (empty($post['id'])) {
            $this->error('至少选择一个房间');
        }

        $data = [];
        $ids  = explode(',', $post['id']);

        foreach ($ids as $k => $v) {
            $status = $this->ur_model->where('id',$v)->value('status');
            if($status == $post['status']){
                $this->error('操作失败，无任何数据变化');
            }
            array_push($data, [
                'id' => $v,
                'status' => $post['status']
            ]);

        }
        $info = $this->ur_model->saveAll($data);
        if (!empty($info)) {
            $title = $post['status'] == 1 ? '正常' : '关闭';

            foreach ($data as $key =>$value) {
                $msg = "管理员[ {$this->username} ]修改房间 [ {$value['id']} ] 状态".$title;
                $this->auth->createLog($msg, $title);
            }
            $this->success('操作成功');
        } else {
            $this->error('操作失败，无任何数据变化');
        }
    }

    //删除
    public function delete()
    {
        $post = input('param.');

        if (empty($post['id'])) {
            $this->error('至少选择一个用户');
        }

        $this->ur_model->destroy($post['id'], true);
        $this->success('删除成功');
    }
}
